package cn.tedu.util;

import org.apache.commons.dbcp.BasicDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Properties;

/**
 * Date：2022年04月
 * Author：程Sir
 * Name： JDBC_project
 * Content:
 *      DBCP连接池管理连接工具类
 */
public class DBCPUtils {
    // 连接池对象
    private static BasicDataSource dataSource;

    public static void main(String[] args) {
        System.out.println(DBCPUtils.class);
    }
    static {
        try {
            InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("dbcp.properties");
            Properties properties = new Properties();
            // 通过加载io流将读取的数据封装到properties对象里面
            properties.load(in);
            // 通过properties对象获取数据
            String driver = properties.getProperty("jdbc.driver");
            String url = properties.getProperty("jdbc.url");
            String username = properties.getProperty("jdbc.username"); //root   当前电脑的用户
            String password = properties.getProperty("jdbc.password");

            // 读取初始连接数和最大连接数
            String initialSize = properties.getProperty("jdbc.initialSize");
            String maxActive = properties.getProperty("jdbc.maxActive");

            System.out.println(url+","+username+","+password + ","+driver +","+initialSize +"," +maxActive);

            // 创建数据库连接池对象
            dataSource = new BasicDataSource();
            // 设置连接池对象的策略参数
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);

            dataSource.setInitialSize(Integer.valueOf(initialSize));
            dataSource.setMaxActive(Integer.valueOf(maxActive));


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接对象
     * @return 返回获得的Connection对象
     */
    public static Connection getConnection(){
        // 通过设置好的连接池对象获取连接对象
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        System.out.println(connection); // 输出时连接对象的地址信息
        return connection;
    }

    /**
     * 将连接对象归还给连接池
     * @param connection 需要归还的连接对象
     */
    public static void close(Connection connection){
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 释放资源
     * @param connection 归还连接对象
     * @param statement 释放执行计划
     * @param resultSet 清除结果集
     */
    public static void close(Connection connection, Statement statement, ResultSet resultSet){
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            resultSet.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


}
